package org.brisling.workflow.queryService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.common.util.JsonUtil;
import org.brisling.workflow.domain.FlowTaskConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/flowTaskConfig/query")
public class QueryTaskconfigService extends AbstractBaseQueryService<FlowTaskConfig>{
	
	//根据业务完整编码和节点名称缓存的节点配置信息
	private static Map<String,FlowTaskConfig> staticMap = new HashMap<String,FlowTaskConfig>() ;
	public QueryTaskconfigService(){
		this.moduleName = ConstSysParam.MODULE_FLOW_TASK_CONFIG;
		this.subSystem = ConstSysParam.SUBSYSTEM_WORKFLOW;
		this.tableName = ConstSysParam.MODULE_FLOW_TASK_CONFIG;
		this.clazz = FlowTaskConfig.class;
		typeMap = this.getTypeMap(clazz);
				
		orderMap.put("sortedid","");			
		
	}
	
	
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return new ModelAndView("/workflow/flowTaskConfig/main");
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return new ModelAndView("/workflow/flowTaskConfig/editDlg");
	}
	
	@PostConstruct
	public void init(){
		
		//清空静态缓存数据
		HashMap<String,Object> maps= SysConfig.getFlowNodeMaps();
		if(maps==null || maps.isEmpty())
			SysConfig.setFlowNodeMaps(this.initMaps());
		
		//清空缓存数据
		staticMap = new HashMap<String,FlowTaskConfig>();
		
	}
	
	/*
	 * 加载所有配置项目
	 */
	@SuppressWarnings("unchecked")
	public HashMap<String,Object> initMaps(){
		
		
		staticMap = new HashMap<String,FlowTaskConfig>();
		
		HashMap<String,Object> maps= new HashMap<String,Object>();
		
		//查询所有流程节点配置
		List<FlowTaskConfig> lists = this.getList();
		
		//循环处理流程节点配置
		for(FlowTaskConfig config:lists){
			
			//获取节点流程配置信息
			String acConfig = config.getActivityConfig();
			
			//节点配置信息是否为空
			if(acConfig!=null && (acConfig.trim().length()>0)){
				//节点配置不为空，配置信息转换为map形式
				Map<String,Map<String,String>> sigalMap =JsonUtil.bodyToObj(config.getActivityConfig(), HashMap.class);
				//用节点id + 完整业务编码拼接为map主键，节点配置信息为map类型键值
				maps.put(config.getActivityId()+config.getFullBizcode(), sigalMap);
			}else{
				//用节点id + 完整业务编码拼接为map主键,键值节点配置信息为空
				maps.put(config.getActivityId()+config.getFullBizcode(), null);
			}
		}
		
		//返回所有节点配置信息
		return maps;
	}
	
	/**
	 * 清除缓存数据
	 * @return
	 */
	@RequestMapping("/reloadConfig.action")
	public String reloadConfig(){
		SysConfig.setFlowNodeMaps(this.initMaps());
		Map<String, Map<String, List<String[]>>>  map = new HashMap<String, Map<String, List<String[]>>> ();
		SysConfig.setOutgoingMap(map);
		return "200";
	}
	
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<FlowTaskConfig> getListDomain(@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap, true);		
	}
	
	
	@RequestMapping("/getPagedList.action")
	public List<FlowTaskConfig> getPagedList(@RequestBody(required=false) String body){		
			
		return getListByConditionAndOrder(body,typeMap,orderMap,true);
	}
	
	
	/**
	 * 获取流程节点配置信息
	 * @param body
	 * @return
	 */
	@RequestMapping("/getTaskConfig.action")
	public Object getTaskConfig(@RequestBody(required=false) String body){
		
		Map<String,Object> paramMap = this.getBodyParam(body);
		return getTaskConfig(paramMap);
	}
	
	/**
	 * 获取流程节点配置信息
	 * @param paramMap
	 * @return
	 */
	
	public Object getTaskConfig(Map<String,Object> paramMap){
		
		
		
		if(paramMap.containsKey("fullBizcode") ){
			String fbc = paramMap.get("fullBizcode").toString();
			
			if(paramMap.containsKey("activityId")){
				String aid = paramMap.get("activityId").toString();
				Map<String,Object> flowMap=SysConfig.getFlowNodeMaps();
				if(flowMap!=null &&flowMap.containsKey(aid+fbc)){
					Object obj = flowMap.get(aid+fbc);
					return obj;
				}
					
				 
			}else{
				String sql = String.format(" select * from %1$s where fullBizcode='%2$s' and activityType=1", this.getTableName(),fbc);
				List<FlowTaskConfig> ftc = super.getAllList(sql);
				if(ftc!=null && ftc.size()>0)
					return JsonUtil.bodyToObj(ftc.get(0).getActivityConfig(), HashMap.class);
			}
			
		}
		return null;
	}
	
	public List<FlowTaskConfig> getPagedList(Map<String, String> map){		
		
		return super.getListByConditionAndOrder(map,typeMap,orderMap,true,true);
	}
	
	public List<FlowTaskConfig> getList(){		
		
		return super.getListByConditionAndOrder(null,typeMap,orderMap,true,false);
	}
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public FlowTaskConfig getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public FlowTaskConfig getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}	
	
	
	@RequestMapping("/getListByField.action")
	public List<FlowTaskConfig> getListByField(@RequestParam(value="field",required=false) String field,@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getListByIdentify(field,fieldVal);
	}
	
	/**
	 * 
	 * @param procDefinitionId	流程定义id
	 * @param activityName		节点名称
	 * @return
	 */
	
	public FlowTaskConfig getByProcDefinitionIdAndActivityName(String procDefinitionId,String activityName){
		FlowTaskConfig ftc = null;
		
		if(procDefinitionId!=null && activityName!=null){
			String _key = procDefinitionId+"_" +activityName;
			
			//判断是否已经缓存数据
			if(staticMap!=null && staticMap.containsKey(_key)){
				return staticMap.get(_key);
			}
			
			//未缓存
			String sql = "select * from %1$s where procDefinitionId = '%2$s' and activityName = '%3$s' ";
			sql = String.format(sql, this.moduleName,procDefinitionId,activityName);
			ftc= super.getOneBySql(sql);
			
			//缓存数据
			if(ftc!=null && ftc.getActivityConfig()!=null)
				staticMap.put(_key, ftc);
		}
		
		return ftc;
	}
	
	/**
	 * 
	 * @param procDefinitionId	流程定义id
	 * @param activityId		节点代码
	 * @return
	 */
	
	public FlowTaskConfig getByProcDefinitionIdAndActivityId(String procDefinitionId,String activityId){
		FlowTaskConfig ftc = null;
		
		if(procDefinitionId!=null && activityId!=null){
			String _key = procDefinitionId+"_" +activityId;
			
			//判断是否已经缓存数据
			if(staticMap!=null && staticMap.containsKey(_key)){
				return staticMap.get(_key);
			}
			
			//未缓存
			String sql = "select * from %1$s where procDefinitionId = '%2$s' and activityId = '%3$s' ";
			sql = String.format(sql, this.moduleName,procDefinitionId,activityId);
			ftc= super.getOneBySql(sql);
			
			//缓存数据
			if(ftc!=null && ftc.getActivityConfig()!=null)
				staticMap.put(_key, ftc);
		}
		
		return ftc;
	}
}
